﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Data.SqlClient;

namespace carsitem.login
{
    public partial class loginForm : Form
    {
        public loginForm()
        {
            InitializeComponent();
        }

        private void loginForm_Load(object sender, EventArgs e)
        {

            //窗口标题设置
            string titleMsg = "亲 ，请 登 录 . . . . . .";
            Graphics g = this.CreateGraphics();
            Double startingPoint = (this.Width / 2) - (g.MeasureString(titleMsg, this.Font).Width / 2);
            Double widthOfASpace = g.MeasureString(" ", this.Font).Width;
            String tmp = " ";
            Double tmpWidth = 0;
            while ((tmpWidth + widthOfASpace) < startingPoint)
            {
                tmp += " ";
                tmpWidth += widthOfASpace;
            }
            this.Text = tmp + titleMsg;
        }

        //登录按钮
        private void Login_Click(object sender, EventArgs e)
        {
            //获取名字
            string EmployeeName = this.textName.Text.Trim();
            //获取密码
            string PassWord = this.textPassword.Text.Trim();
            //获取部门
            string Department = this.comboDepartment.Text.Trim();


            bool result = LoginValidation.CheckLogin(EmployeeName, PassWord, Department);
        
      
            if (result)
            {
                MessageBox.Show("登录成功!");
                carsitem.MainInterface.MainInterfaceForm mainInterfaceForm = new carsitem.MainInterface.MainInterfaceForm();//把form1传给form2
                this.comboDepartment.Enabled = false;
                mainInterfaceForm.Show();
                this.Hide();//把自己隐藏了
            }
            else
            {
                MessageBox.Show("登录失败!");
            }
        }

        //退出按钮
        private void exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        //退出程序
        private void LiginForm_Load(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        //汉字验证
        public bool IsChineseCharacters(string oText)
        {
            string text = @"[\u4E00-\u9FA5]+$";
            if (oText != "")
            {
                if (!Regex.IsMatch(oText.ToString(), text))
                    MessageBox.Show("请输入汉字！");
            }
            return true;
        }

        //表单验证
        private void textName_Leave(object sender, EventArgs e)//用户名
        {
            string oText = textName.Text.Trim();
            IsChineseCharacters(oText);
        }

        private void textPassword_Leave(object sender, EventArgs e)//密码
        {
            if (textPassword.Text.Trim() == "")
            {
                MessageBox.Show("请输入密码");
            }
        }
    }
}
